package com.elmcode.server.controller.admin;

import com.elmcode.common.utils.PageUtils;
import com.elmcode.common.utils.R;
import com.elmcode.server.dto.CategoryDTO;
import com.elmcode.server.entity.CategoryEntity;
import com.elmcode.server.exception.ParamsException;
import com.elmcode.server.interceptor.LoginInterceptor;
import com.elmcode.server.service.CategoryService;
import com.elmcode.server.utils.Result;
import com.elmcode.server.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 菜品分类
 *
 * @author laozj
 * @email laozj@elm.com
 * @date 2024-06-13 13:05:04
 */
@RestController
@Api(tags = "分类接口")
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加用户
     */
    @ApiOperation("添加分类接口")
    @PostMapping
    public Result addCategory(CategoryDTO categoryDTO){
        categoryService.saveCategory(categoryDTO);
        return Result.ok("添加成功");
    }


    /**
     * 查询用户
     */
    @GetMapping
    @ApiOperation("查询分类接口")
    public Result list(@RequestParam Map<String, Object> params){

        /**
         * mybatis分页插件
         */
        PageUtils page = categoryService.queryPage(params);
        return Result.ok("获取用户成功",page.getList(),page.getTotal());
    }

    /**
     * 更新/编辑用户接口
     */
    @ApiOperation("更新/编辑分类接口")
    @PutMapping("/{id}")
    public Result update(@PathVariable("id")Long id, CategoryDTO categoryDTO){
        categoryService.updateCategoryInfo(id,categoryDTO);

        return Result.ok("更新成功");
    }

    /**
     * 删除分类接口
     */
    @ApiOperation("删除分类接口")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id")Long id){
        categoryService.deleteById(id);

        return Result.ok("删除成功");
    }

    /**
     * 禁用/启用分类接口
     */
    @ApiOperation("禁用/启用分类接口")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable("id")Long id,@RequestParam String status) throws ParamsException {
        categoryService.updateStatus(id,status);

        return Result.ok("更新状态成功");
    }



    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryEntity category = categoryService.getById(id);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
